using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MKDS_Course_Editor.GCN.BTI
{
    public partial class BTI : Form
    {
        string file;
        Chadsoft.CTools.Image.Bti.BtiImage b;
        public BTI(string file)
        {
            this.file = file;
            InitializeComponent();
        }

        private void BTI_Load(object sender, EventArgs e)
        {
            b = new Chadsoft.CTools.Image.Bti.BtiImage(File.Open(file, FileMode.Open, FileAccess.Read));
            for (int i = 0; i < b.Levels; i++)
            {
                toolStripComboBox1.Items.Add(i + 1);
            }
            toolStripComboBox1.SelectedIndex = 0;
            pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {
                pictureBox1.Image.Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.Import(Chadsoft.CTools.Image.ImageData.GetData(bi),b.GetFormats()[toolStripComboBox1.SelectedIndex], b.Levels, bi.Width, bi.Height);
                pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
            }
        }

        private void i4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.Import(Chadsoft.CTools.Image.ImageData.GetData(bi), Chadsoft.CTools.Image.ImageDataFormat.I4, b.Levels, bi.Width, bi.Height);
                pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
            }
        }

        private void i8ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.Import(Chadsoft.CTools.Image.ImageData.GetData(bi), Chadsoft.CTools.Image.ImageDataFormat.I8, b.Levels, bi.Width, bi.Height);
                pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
            }
        }

        private void iA4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.Import(Chadsoft.CTools.Image.ImageData.GetData(bi), Chadsoft.CTools.Image.ImageDataFormat.IA4, b.Levels, bi.Width, bi.Height);
                pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
            }
        }

        private void iA8ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.Import(Chadsoft.CTools.Image.ImageData.GetData(bi), Chadsoft.CTools.Image.ImageDataFormat.IA8, b.Levels, bi.Width, bi.Height);
                pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
            }
        }

        private void rGB565ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.Import(Chadsoft.CTools.Image.ImageData.GetData(bi), Chadsoft.CTools.Image.ImageDataFormat.RGB565, b.Levels, bi.Width, bi.Height);
                pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
            }
        }

        private void rGB5A5ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.Import(Chadsoft.CTools.Image.ImageData.GetData(bi), Chadsoft.CTools.Image.ImageDataFormat.RGB5A3, b.Levels, bi.Width, bi.Height);
                pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
            }
        }

        private void rgba32ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.Import(Chadsoft.CTools.Image.ImageData.GetData(bi), Chadsoft.CTools.Image.ImageDataFormat.Rgba32, b.Levels, bi.Width, bi.Height);
                pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
            }
        }

        private void cmprToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                Bitmap bi = new Bitmap(openFileDialog1.FileName);
                b.Import(Chadsoft.CTools.Image.ImageData.GetData(bi), Chadsoft.CTools.Image.ImageDataFormat.Cmpr, b.Levels, bi.Width, bi.Height);
                pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            b.Save(File.OpenWrite(file));
        }

        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = Chadsoft.CTools.Image.ImageData.ToBitmap(b.GetData(toolStripComboBox1.SelectedIndex), b.Width, b.Height);
        }
    }
}
